package com.kye.express.business.order_list;

import com.kye.express.business.order.NameCertificationUtils;
import com.kye.express.contract.base.BasePresenter;
import com.kye.express.contract.base.view.ErrorView;
import com.kye.express.contract.base.view.IView;
import com.kye.express.contract.base.view.LoadingView;
import com.kye.model.OpenApiResultObserver;
import com.kye.model.SharedPreferencesConfig;
import com.kye.model.SharedPreferencesManager;
import com.kye.model.bean.OpenApiResult;
import com.kye.model.bean.order.OrderValidationNameCertificationBean;
import com.kye.model.bean.order.scan_code_order.IsCanScanCodeToAddOrder;
import com.kye.model.bean.order.scan_code_order.ScanCodeOrderAddress;
import com.kye.model.datasource.OrderDataSource;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public interface OrderListContract {

    interface OrderListView extends IView, LoadingView, ErrorView {

        void onNeedValidation(OrderValidationNameCertificationBean validation, NameCertificationUtils.CheckListener.Type type);

        void onCanScanCodeAddOrder(String companyCode, ScanCodeOrderAddress addressData);

        void onCanNotScanCodeAddOrder();
    }

    class OrderListPresenter extends BasePresenter<OrderListView> {

        private final OrderDataSource mOrderDataSource;

        public OrderListPresenter() {
            mOrderDataSource = new OrderDataSource();
        }

        public void checkNameCertification(NameCertificationUtils.CheckListener.Type type) {
            mOrderDataSource.checkNameCertification()
                    .compose(getView().bindToLifecycleDestroy())
                    .doOnSuccess(new Consumer<OpenApiResult<OrderValidationNameCertificationBean>>() {
                        @Override
                        public void accept(OpenApiResult<OrderValidationNameCertificationBean> orderValidationNameCertificationBeanOpenApiResult) throws Exception {
                            SharedPreferencesManager.commit(SharedPreferencesConfig.KEY_NAME_CERTIFICATION, System.currentTimeMillis());
                        }
                    })
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new OpenApiResultObserver<OrderValidationNameCertificationBean>() {
                        @Override
                        public void onLoading(Disposable d) {
                            getView().onLoading();
                        }

                        @Override
                        public void onSuccess(OpenApiResult<OrderValidationNameCertificationBean> result, OrderValidationNameCertificationBean orderValidationNameCertificationBean) {
                            getView().onNeedValidation(orderValidationNameCertificationBean, type);
                        }

                        @Override
                        public void onApiError(OpenApiResult<OrderValidationNameCertificationBean> result, int code, String msg) {
                            getView().onError();
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().onError();
                        }

                        @Override
                        public void onLoaded() {
                            getView().onLoaded();
                        }
                    });
        }

        public void checkCustomCodeForStartScanCodeAddOrder(String customCode) {
            mOrderDataSource.isCanAddOrder(customCode, null)
                    .compose(getView().bindToLifecycleDestroy())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new OpenApiResultObserver<IsCanScanCodeToAddOrder>() {
                        @Override
                        public void onLoading(Disposable d) {
                            getView().onLoading();
                        }

                        @Override
                        public void onSuccess(OpenApiResult<IsCanScanCodeToAddOrder> result, IsCanScanCodeToAddOrder isCanScanCodeToAddOrder) {
                            if (isCanScanCodeToAddOrder.isOrdersType()) {
                                final ScanCodeOrderAddress addressData = new ScanCodeOrderAddress();
                                addressData.setContactsPhone(isCanScanCodeToAddOrder.getContactNumber());
                                addressData.setContacts(isCanScanCodeToAddOrder.getContacts());
                                addressData.setIsDefault(isCanScanCodeToAddOrder.isDefault());
                                addressData.setProvince(isCanScanCodeToAddOrder.getProvince());
                                addressData.setCity(isCanScanCodeToAddOrder.getCity());
                                addressData.setArea(isCanScanCodeToAddOrder.getArea());
                                addressData.setAddress(isCanScanCodeToAddOrder.getAddress());
                                addressData.setGuid(isCanScanCodeToAddOrder.getGuid());
                                getView().onCanScanCodeAddOrder(isCanScanCodeToAddOrder.getCompanyCode(), addressData);
                            } else {
                                getView().onCanNotScanCodeAddOrder();
                            }
                        }

                        @Override
                        public void onApiError(OpenApiResult<IsCanScanCodeToAddOrder> result, int code, String msg) {
                            getView().onError(msg);
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().onError();
                        }

                        @Override
                        public void onLoaded() {
                            getView().onLoaded();
                        }
                    });
        }

    }
}
